﻿
function pageLoad(sender, args)
{
    ShowLeft();
}
//验证字数
function fucCheckLength(strTemp)
{
    var i,sum;
    sum=0;
    for(i=0;i<strTemp.length;i++)
    {
        if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255))
        sum=sum+1;
        else
        sum=sum+2;
    }
    return sum;
}
function ShowLeft()
{
    var LenString,LenStringI,Strings;
    LenString=fucCheckLength($("QuesText").value);
    LenStringI=LenString;
    if (LenString>110)
    {
        Strings=$("QuesText").value;
        while(LenStringI>110)
        {
            if ((Strings.charCodeAt(Strings.length)>=0) && (Strings.charCodeAt(Strings.length)<=255))
            {
                LenStringI=LenStringI-1;
            }
            else
            {
                LenStringI=LenStringI-2;
            }
            Strings=Strings.substring(0,(Strings.length-1));
        }
        $("QuesText").value=Strings;
        return false;
    }
    $("charremain").innerHTML=110-LenString;
}
//添加问题
function AddQuestion()
{    
    var varQues = $("QuesText").value;
    var varQuesDes = $("QuesTextarea").value;
    var varLearn = $("selLearnL").value;
    var varSpeak = $("selSpeakL").value;
    var varTags = "";
    var elments = $("tbTag").getElementsByTagName("input");
    for (var i = 0; i < elments.length; i++)
    {
        if(elments[i].type=="checkbox" && elments[i].checked)
        {
            varTags += elments[i].value+" ";
        }
    }
    var varEmailNotify = $("CEmailNotify").checked;
    if(varQues.length<10 || varQues.length>110)
    {
        $("ErrorQue").style.display = "";
        return;
    }
    $("ErrorQue").style.display = "none";
    if(varQuesDes.length>1001)
    {
        $("ErrorQueDes").style.display = "";
        return;
    }
    $("ErrorQueDes").style.display = "none";
    if(varLearn=="")
    {
        $("ErrorLearn").style.display = "";
        return;
    }
    $("ErrorLearn").style.display = "none";
    if(varSpeak=="")
    {
        $("ErrorBase").style.display = "";
        return;
    }
    $("ErrorBase").style.display = "none";
    //tag验证 
    var tags = $("TagsText").value;
    var tagArr = tags.split(" ");    
    if(tags.indexOf("-")!=-1||tags.indexOf("/")!=-1)
    {
        $("ErrorTags").style.display = "";
        return;
    }
    for(var i = tagArr.length-1; i >=0; i--)
    {
        if(tagArr[i].length > 50)
        {
            $("ErrorTags").style.display = "";
            return;
        }
    }    
    varTags += tags;
    if(varTags.split(" ").length>25)
    {
        $("ErrorTags").style.display = "";
        return;
    }
    $("ErrorTags").style.display = "none";
    $get("AddButton").disabled = true;
    WebServices.WWW.Answers.AddAskQuestion(varQues,varQuesDes,varLearn,varSpeak,varTags,varEmailNotify,1,onsuccOP);    
}
function onsuccOP(result)
{    
    var questionID = result.Effect;
    if(questionID != 0)
    {
        top.location = "/answers/question/"+questionID+"-1-1-1.htm";
    }
    else{
         $get("AddButton").disabled = false;
    }   
}